'use client' import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { Badge } from '@/components/ui/badge' import { Search, Check } from 'lucide-react' import { ColumnDef, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, SortingState, ColumnFiltersState, VisibilityState, RowSelectionState, } from '@tanstack/react-table' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table' import { getContracts } from '../actions/data-actions' import { toast } from 'sonner' interface Contract { id: number contractNo: string contractName: string status: string projectId: number vendorId: number projectCode: string | null projectName: string | null vendorName: string | null vendorCode: string | null } interface ContractSelectorProps { selectedContract?: Contract onContractSelect: (contract: Contract) => void disabled?: boolean preselectedContractId?: number } export function ContractSelector({ selectedContract, onContractSelect, disabled, preselectedContractId }: ContractSelectorProps) { const [open, setOpen] = useState(false) const [contracts, setContracts] = useState([]) const [loading, setLoading] = useState(false) const [sorting, setSorting] = useState([]) const [columnFilters, setColumnFilters] = useState([]) const [columnVisibility, setColumnVisibility] = useState({}) const [rowSelection, setRowSelection] = useState({}) const [globalFilter, setGlobalFilter] = useState('') const columns: ColumnDef[] = [ { accessorKey: 'contractNo', header: '계약번호', cell: ({ row }) => (
{row.getValue('contractNo')}
), }, { accessorKey: 'contractName', header: '계약명', cell: ({ row }) => (
{row.getValue('contractName')}
), }, { accessorKey: 'status', header: '상태', cell: ({ row }) => { const status = row.getValue('status') as string const getStatusColor = (status: string) => { switch (status) { case 'ACTIVE': return 'bg-green-100 text-green-800' case 'TEST': return 'bg-blue-100 text-blue-800' case 'DRAFT': return 'bg-yellow-100 text-yellow-800' case 'PENDING': return 'bg-orange-100 text-orange-800' default: return 'bg-gray-100 text-gray-800' } } return ( {status} ) }, }, { accessorKey: 'projectCode', header: '프로젝트', cell: ({ row }) => { const projectCode = row.getValue('projectCode') as string | null const projectName = row.original.projectName return projectCode ? (
{projectCode}
{projectName && (
{projectName}
)}
) : ( - ) }, }, { accessorKey: 'vendorName', header: '벤더', cell: ({ row }) => { const vendorName = row.getValue('vendorName') as string | null const vendorCode = row.original.vendorCode return vendorName ? (
{vendorName}
{vendorCode && (
{vendorCode}
)}
) : ( - ) }, }, { id: 'actions', header: '선택', cell: ({ row }) => ( ), }, ] const table = useReactTable({ data: contracts, columns, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, onColumnVisibilityChange: setColumnVisibility, onRowSelectionChange: setRowSelection, onGlobalFilterChange: setGlobalFilter, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), state: { sorting, columnFilters, columnVisibility, rowSelection, globalFilter, }, }) const loadContracts = async () => { setLoading(true) try { const result = await getContracts() if (result.success) { setContracts(result.data) // preselectedContractId가 있으면 자동 선택 if (preselectedContractId && !selectedContract) { const preselectedContract = result.data.find(c => c.id === preselectedContractId) if (preselectedContract) { onContractSelect(preselectedContract) } } } else { toast.error(result.error) } } catch (error) { toast.error('계약을 불러오는 중 오류가 발생했습니다.') } finally { setLoading(false) } } const handleContractSelect = (contract: Contract) => { onContractSelect(contract) setOpen(false) } useEffect(() => { if (open && contracts.length === 0) { loadContracts() } }, [open]) // preselectedContractId가 변경되면 계약 목록 다시 로드 useEffect(() => { if (preselectedContractId && contracts.length === 0) { loadContracts() } }, [preselectedContractId]) return ( 계약 선택
setGlobalFilter(e.target.value)} className="flex-1" />
{loading ? (
계약을 불러오는 중...
) : (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( handleContractSelect(row.original)} > {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ))} )) ) : ( 검색 결과가 없습니다. )}
)}
총 {table.getFilteredRowModel().rows.length}개 계약
{table.getState().pagination.pageIndex + 1} / {table.getPageCount()}
) }